import request from '../utils/request'

const API_BASE = '/v1/jsonrpc2'

export default {
  /**
   * 搜索录像文件
   * @param {Object} params 搜索参数
   * @param {number} params.channel 通道号
   * @param {string} params.start_time 开始时间(ISO 8601格式)
   * @param {string} params.end_time 结束时间(ISO 8601格式)
   * @param {string} params.event_type 事件类型: "all"/"motion"/"alarm"/"manual"/"schedule"
   * @param {number} params.stream_type 码流类型: 0(主码流)/1(子码流)
   * @returns {Promise}
   */
  async searchRecords(params) {
    const data = {
      id: `records-search-${Date.now()}`,
      method: 'records.search',
      params
    }
    
    const response = await request.post(API_BASE, data)
    return response.data
  },

  /**
   * 获取录像文件列表
   * @param {Object} params 参数
   * @param {string} params.search_id 搜索会话ID(可选)
   * @param {number} params.page 页码，从1开始
   * @param {number} params.page_size 每页条数
   * @param {string} params.sort_by 排序字段: "start_time"/"duration"/"size"
   * @param {string} params.sort_order 排序方向: "asc"/"desc"
   * @returns {Promise}
   */
  async getRecordList(params) {
    const data = {
      id: `records-list-${Date.now()}`,
      method: 'records.getList',
      params
    }
    
    const response = await request.post(API_BASE, data)
    return response.data
  },

  /**
   * 获取录像文件详细信息
   * @param {string} fileId 文件ID
   * @returns {Promise}
   */
  async getRecordInfo(fileId) {
    const data = {
      id: `records-info-${Date.now()}`,
      method: 'records.getInfo',
      params: {
        file_id: fileId
      }
    }
    
    const response = await request.post(API_BASE, data)
    return response.data
  },

  /**
   * 开始回放
   * @param {Object} params 回放参数
   * @param {string} params.file_id 文件ID
   * @param {string} params.protocol 流协议: "webrtc"/"rtsp"
   * @param {number} params.start_position 起始播放位置(秒)，相对于录像开始时间
   * @param {number} params.speed 播放速度倍数
   * @returns {Promise}
   */
  async startPlayback(params) {
    const data = {
      id: `playback-start-${Date.now()}`,
      method: 'playback.start',
      params
    }
    
    const response = await request.post(API_BASE, data)
    return response.data
  },

  /**
   * 停止回放
   * @param {string} playbackId 回放会话ID
   * @returns {Promise}
   */
  async stopPlayback(playbackId) {
    const data = {
      id: `playback-stop-${Date.now()}`,
      method: 'playback.stop',
      params: {
        playback_id: playbackId
      }
    }
    
    const response = await request.post(API_BASE, data)
    return response.data
  },

  /**
   * 暂停回放
   * @param {string} playbackId 回放会话ID
   * @returns {Promise}
   */
  async pausePlayback(playbackId) {
    const data = {
      id: `playback-pause-${Date.now()}`,
      method: 'playback.pause',
      params: {
        playback_id: playbackId
      }
    }
    
    const response = await request.post(API_BASE, data)
    return response.data
  },

  /**
   * 恢复回放
   * @param {string} playbackId 回放会话ID
   * @returns {Promise}
   */
  async resumePlayback(playbackId) {
    const data = {
      id: `playback-resume-${Date.now()}`,
      method: 'playback.resume',
      params: {
        playback_id: playbackId
      }
    }
    
    const response = await request.post(API_BASE, data)
    return response.data
  },

  /**
   * 定位回放位置
   * @param {Object} params 定位参数
   * @param {string} params.playback_id 回放会话ID
   * @param {number} params.position 目标位置(秒)，相对于录像开始时间
   * @param {string} params.seek_type 定位类型: "absolute"/"relative"
   * @returns {Promise}
   */
  async seekPlayback(params) {
    const data = {
      id: `playback-seek-${Date.now()}`,
      method: 'playback.seek',
      params
    }
    
    const response = await request.post(API_BASE, data)
    return response.data
  },

  /**
   * 设置回放速度
   * @param {Object} params 速度参数
   * @param {string} params.playback_id 回放会话ID
   * @param {number} params.speed 速度倍数
   * @returns {Promise}
   */
  async setPlaybackSpeed(params) {
    const data = {
      id: `playback-speed-${Date.now()}`,
      method: 'playback.setSpeed',
      params
    }
    
    const response = await request.post(API_BASE, data)
    return response.data
  }
}